//
//  MineViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "MineViewController.h"
#import "DSMineTableViewCell.h"
#import "FriendsListViewController.h"
#import "Qr codeViewController.h"
#import "ZYCollectionViewController.h"
static NSString *const Ecell = @"Ecell";
//static NSString *const Rcell = @"Rcell";

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *myTable;

@property(nonatomic, retain)UIView *bigView;
@property(nonatomic, strong)UIVisualEffectView *effect;//磨玻璃
@property(nonatomic, retain)NSMutableArray *myArray;//列表数组

@end

@implementation MineViewController

-(void)dealloc{
    [_bigView release];
    [_myTable release];
    
    [_labelD release];
    [_buttonJ release];
    [_buttonP release];
    [_imageV release];
    [_effect release];
    
    [_myArrayA release];
    [_myArrayA1 release];
    [_myArrayA2 release];
    [_myArrayA3 release];
    [_myArrayA4 release];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"username" object:nil];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    //通知 用户名回调
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userName:) name:@"username" object:nil];
    
    
    

    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    [self getData];
    self.bigView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.bigView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bigView];
    
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, -50, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor clearColor];
    [self.bigView addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;

    // 注册
    [self.myTable registerNib:[UINib nibWithNibName:@"DSMineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:Ecell];
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self creatTou];
    
    [self.bigView release];

    // Do any additional setup after loading the view.
}

-(void)userName:(NSNotificationCenter *)noti{
    
    
    [self dismissViewControllerAnimated:YES completion:^{
    
    }];
}
//创建头视图
-(void)creatTou{

    self.imageV = [[UIImageView alloc] init];
    self.imageV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.imageV];
    self.imageV.frame = CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height/3);
    self.imageV.userInteractionEnabled = YES;
    self.imageV.image = [UIImage imageNamed:@"102"];
    
//手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    tapGesture.numberOfTouchesRequired = 1;//响应事件的手指数
    tapGesture.numberOfTapsRequired = 1;//响应事件的点击次数
    [self.imageV addGestureRecognizer:tapGesture];
    
//磨砂玻璃效果
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effect.frame = CGRectMake(0, 0, self.imageV.frame.size.width, self.imageV.frame.size.height);
    self.effect.alpha = 0.5;
    [self.imageV addSubview:self.effect];
    
    self.buttonP = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(20, self.imageV.frame.size.height/3, self.imageV.frame.size.height/3, self.imageV.frame.size.height/3) title:nil image:[UIImage imageNamed:@"120"] color:[UIColor colorWithRed:245/256.0 green:244/256.0 blue:248/256.0 alpha:1] handler:^(UIButton *sender) {
        
        NSLog(@"请登录");
    }];
    self.buttonP.layer.masksToBounds = YES;
    self.buttonP.layer.cornerRadius = self.imageV.frame.size.height/6;
    [self.imageV addSubview:self.buttonP];
    
    self.labelD = [FengLabel LabelwithFrame:CGRectMake(self.buttonP.frame.size.width+40, self.imageV.frame.size.height/2-10, self.imageV.frame.size.width/2, self.buttonP.frame.size.height/3) Color:[UIColor clearColor] font:18];
    self.labelD.text = @"请登录";
    [self.imageV addSubview:self.labelD];
    
    self.buttonJ = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.labelD.frame.size.width+self.labelD.frame.origin.x+20, self.imageV.frame.size.height/2-10, self.buttonP.frame.size.width/3, self.buttonP.frame.size.width/3) title:nil image:[UIImage imageNamed:@"103"] color:[UIColor clearColor] handler:^(UIButton *sender) {
        
        NSLog(@"登录");
    }];
    [self.imageV addSubview:self.buttonJ];
    
    self.myTable.tableHeaderView = self.imageV;
    
    [self.labelD release];
    [self.imageV release];
    [self.effect release];
}
// 数据
-(void)getData
{
    self.myArrayA = [NSMutableArray array];
    self.myArrayA1 = [NSMutableArray array];
    self.myArrayA2 = [NSMutableArray array];
    self.myArrayA3 = [NSMutableArray array];
    self.myArrayA4 = [NSMutableArray array];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"夜间模式", @"我的音乐", @"我的好友", @"扫一扫", @"二维码", @"清除缓存",nil];
    for (int i = 0; i < 6; i++) {
        MineModel *model = [[MineModel alloc] init];
        model.title = [array objectAtIndex:i];
        model.photo = [NSString stringWithFormat:@"btn_mine_icon%d",i + 1];
        if (i < 1) {
            [self.myArrayA1 addObject:model];
        }else if (i >= 1 && i < 3) {
            [self.myArrayA2 addObject:model];
        }else if (i >= 3 && i < 5) {
            [self.myArrayA3 addObject:model];
        }else if (i >= 5 && i < 6) {
            [self.myArrayA4 addObject:model];
        }
    }
    [self.myArrayA addObject:self.myArrayA1];
    [self.myArrayA addObject:self.myArrayA2];
    [self.myArrayA addObject:self.myArrayA3];
    [self.myArrayA addObject:self.myArrayA4];
    NSLog(@"%lu", (unsigned long)self.myArrayA.count);
}
// 分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.myArrayA.count;
}
// 分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

// Cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.myArrayA1.count;
    }
    if (section == 1) {
        return self.myArrayA2.count;
    }
    if (section == 2) {
        return self.myArrayA3.count;
    }
    return self.myArrayA4.count;
}
// cell显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DSMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Ecell];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF, 0x828282);
    
    if (indexPath.section == 0) {
        MineModel *model = self.myArrayA1[indexPath.row];
        cell.mineModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 1) {
        MineModel *model = self.myArrayA2[indexPath.row];
        cell.mineModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 2) {
        MineModel *model = self.myArrayA3[indexPath.row];
        cell.mineModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MineModel *model = self.myArrayA4[indexPath.row];
    cell.mineModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}
// cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
// 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
            if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
                NSLog(@"白天");
                [self.dk_manager dawnComing];
            }else{
                NSLog(@"晚上");
                [self.dk_manager nightFalling];
            }
            break;
        case 1:
            if (indexPath.row == 0) {
                 NSLog(@"我的音乐");
                ZYCollectionViewController *zyVC = [[ZYCollectionViewController alloc] init];
                UINavigationController *NA = [[UINavigationController alloc] initWithRootViewController:zyVC];
                [self presentViewController:NA animated:YES completion:^{
                    
                    
                }];
                [zyVC release];
                [NA release];
            }else if (indexPath.row == 1) {
                NSLog(@"我的好友");
                FriendsListViewController *FrindsVC = [FriendsListViewController new];
                UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:FrindsVC];
                [self presentViewController:naVC animated:YES completion:^{
                    
                    
                }];
            }
            break;
        case 2:
            if (indexPath.row == 0) {
                [self tiaoSaoVc];
                NSLog(@"扫一扫");
            }else if (indexPath.row == 1) {
                NSLog(@"二维码");
                Qr_codeViewController *QrVC = [Qr_codeViewController new];
                UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:QrVC];
                
                [self presentViewController:naVC animated:YES completion:^{
                    
                    
                }];
                [naVC release];
            }
            break;
        case 3:
            [self chuHuan];
            NSLog(@"清除缓存");
            break;
        
        default:
            break;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = DSRGBA(235, 235, 235, 1);
    return view;
    
}
//调到扫一扫页面
-(void)tiaoSaoVc{
    SaoViewController *saoVc = [[SaoViewController alloc] init];
//    [self presentViewController:saoVc animated:YES completion:^{
//        
//        
//    }];
    [self.navigationController pushViewController:saoVc animated:YES];
}

-(void)chuHuan{
    UIAlertController *alerAction = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"确定删除%.2f M缓存吗?",[self clearCache]] message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alerAction addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alerAction addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self removedChace];
        [self.myTable reloadData];
        
    }]];
    
    [self presentViewController:alerAction animated:YES completion:nil];
}

//找到缓存路径,读取缓存
-(float)clearCache{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    float folderSize;
    
    if ([fileManager fileExistsAtPath:path]) {
        //拿到有文件的数组
        NSArray *childerFile = [fileManager subpathsAtPath:path];
        //拿到每个文件的名字,如果有不想清除的文件,就在这里判断
        for (NSString *fileName in childerFile) {
            //将路径拼接到一起
            NSString *fullPath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizePath:fullPath];
        }
    }
    return folderSize;
}

//缓存的计算
-(float)fileSizePath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

//清除缓存
-(void)removedChace{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如果有需要,加入条件过滤掉不想删除的文件
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}
//Alert提示自动消失
-(void)creatAlert:(NSTimer *)timer{
    UIAlertController *alert = [timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
    
}
//手势跳转登录
-(void)gesture:(UITapGestureRecognizer *)gesture{
    MineDengViewController *mdVc = [[MineDengViewController alloc] init];
    
   [self presentViewController:mdVc animated:YES completion:^{
    
    
    }];
    [mdVc release];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
